package com.zkingsoft.services.distribution.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zkingsoft.dao.distribution.DisCouponDao;
import com.zkingsoft.dao.distribution.DisDesignerDao;
import com.zkingsoft.dao.distribution.DisMembersDao;
import com.zkingsoft.dao.distribution.DisRebateFlowDao;
import com.zkingsoft.exception.GlobleException;
import com.zkingsoft.model.distribution.DisCoupon;
import com.zkingsoft.model.distribution.DisDesigner;
import com.zkingsoft.model.distribution.DisMembers;
import com.zkingsoft.model.distribution.DisRebateFlow;
import com.zkingsoft.pojo.PaginationVO;
import com.zkingsoft.services.distribution.DisRebateFlowService;
import com.zkingsoft.util.ModelUtils;

/**
 * This field was generated by Zking.software.Codegen.
 * 
 * @date 2017-06-26 07:15
 */
@Service("disRebateFlowService")
public class DisRebateFlowServiceImpl implements DisRebateFlowService {

	@Autowired
	private DisRebateFlowDao disRebateFlowDao;
	
	@Autowired
	private DisDesignerDao disDesignerDao;
	
	@Autowired
	private DisMembersDao disMembersDao;
	
	@Autowired
	private DisCouponDao disCouponDao;

	@Override
	public int add(DisRebateFlow disRebateFlow) {

		return disRebateFlowDao.insert(disRebateFlow);

	}

	@Override
	public int batchAdd(List<DisRebateFlow> disRebateFlowList) {
		int num = 0;

		int c = 10000;
		int size = disRebateFlowList.size() / c + 1;
		for (int i = 0; i < size; i++) {
			int begin = i * c;
			int end = (i + 1) * c;
			end = end >= disRebateFlowList.size() ? disRebateFlowList.size()
					: end;
			List<DisRebateFlow> insertList = disRebateFlowList.subList(begin,
					end);
			num += disRebateFlowDao.batchInsert(insertList);
		}
		return num;

	}

	@Override
	public int modifyByMap(DisRebateFlow oldDisRebateFlow,
			DisRebateFlow newDisRebateFlow) {

		if (!ModelUtils.isModified(oldDisRebateFlow, newDisRebateFlow)) {
			return 1;
		}
		Map<String, Object> modifyMap = null;
		try {
			modifyMap = ModelUtils.comparePojo2Map(oldDisRebateFlow,
					newDisRebateFlow);
		} catch (Exception e) {
			throw new GlobleException("数据对比失败");
		}
		if (modifyMap.size() > 0) {
			modifyMap.put("flowId", oldDisRebateFlow.getFlowId());
			disRebateFlowDao.updateByMap(modifyMap);
		}
		return 1;
	}

	@Override
	public int modifyByModel(DisRebateFlow disRebateFlow) {

		return disRebateFlowDao.updateByModel(disRebateFlow);

	}

	@Override
	public int remove(List<Long> list) {

		return disRebateFlowDao.deleteByIds(list);

	}

	@Override
	public int removeById(Long flowId) {

		return disRebateFlowDao.deleteById(flowId);

	}

	@Override
	public int removeByModel(DisRebateFlow disRebateFlow) {

		return disRebateFlowDao.deleteByModel(disRebateFlow);

	}

	@Override
	public List<DisRebateFlow> findInPage(DisRebateFlow disRebateFlow,
			PaginationVO pageVo) {

		return disRebateFlowDao.selectInPage(disRebateFlow, pageVo);

	}

	@Override
	public List<DisRebateFlow> findByModel(DisRebateFlow disRebateFlow) {

		return disRebateFlowDao.selectByModel(disRebateFlow);

	}

	@Override
	public int findTotal(DisRebateFlow disRebateFlow) {

		return disRebateFlowDao.selectTotalRecord(disRebateFlow);

	}

	@Override
	public DisRebateFlow findById(Long flowId) {

		return disRebateFlowDao.selectById(flowId);

	}
	
	@Override
	public List<DisRebateFlow> findRebateFlows(Long desId, String month) {
		
		return disRebateFlowDao.selectRebateFlows(desId, month);
	}
	
	@Override
	public Map<String, BigDecimal> findTotalRebate(Long desId, String month) {
		
		return disRebateFlowDao.selectTotalRebate(desId, month);
	}

	@Override
	public List<String> findAllMonth() {
		
		return disRebateFlowDao.selectAllMonth();
	}
	
	@Override
	@Transactional(rollbackFor=Exception.class)
	public int addOrder(DisRebateFlow disRebateFlow) {
		//更新优惠券
		DisCoupon disCoupon = new DisCoupon();
		disCoupon.setCouponCode(disRebateFlow.getFlowCoupon());
		List<DisCoupon> list = disCouponDao.selectByModel(disCoupon);
		if(list == null || list.isEmpty()) {
			throw new GlobleException("优惠码错误！");
		}
		disCoupon = list.get(0);
		if("3".equals(disCoupon.getCouponState())) {
			throw new GlobleException("优惠码已被使用！");
		}
		disCoupon.setMbId(disRebateFlow.getMbId());
		disCoupon.setCouponActivateTime(new Date());
		disCoupon.setCouponState("3");
		disCouponDao.updateByModel(disCoupon);
		
		DisMembers disMembers = disMembersDao.selectById(disRebateFlow.getMbId());
		disRebateFlow.setFlowCustomerTel(disMembers.getMbTel());
		disRebateFlow.setFlowCustomerWx(disMembers.getMbWechat());

		//上级设计师
		DisDesigner disDesigner = disDesignerDao.selectById(disMembers.getDesId());
		BigDecimal desRebateProportion = new BigDecimal(disDesigner.getDesRebateProportion());
		disRebateFlow.setDesId(disDesigner.getDesId());
		//返点给上级
		BigDecimal flowRebate = disRebateFlow.getFlowConsume().multiply(desRebateProportion).divide(new BigDecimal(100), 2);
		
		//上级的上级设计师
		BigDecimal flowRebateSub = BigDecimal.ZERO;
		DisDesigner parDisDesigner = disDesignerDao.selectById(disDesigner.getDesParentId());
		if(parDisDesigner != null && parDisDesigner.getDesSubProportion() != null) {
			BigDecimal desSubProportion = BigDecimal.valueOf(parDisDesigner.getDesSubProportion());
			//返点给上级的上级
			flowRebateSub = flowRebate.multiply(desSubProportion).divide(new BigDecimal(100), 2);
		}
		disRebateFlow.setFlowRebate(flowRebate);
		disRebateFlow.setFlowRebateSub(flowRebateSub);
		disRebateFlow.setFlowTime(new Date());
		
		int i = 0;
		i += disRebateFlowDao.insert(disRebateFlow);
		
		disDesigner.setDesRebate(disDesigner.getDesRebate().add(disRebateFlow.getFlowRebate()));
		i += disDesignerDao.updateByModel(disDesigner);
		
		if(parDisDesigner != null && BigDecimal.ZERO.compareTo(disRebateFlow.getFlowRebateSub()) == -1) {
			parDisDesigner.setDesRebate(parDisDesigner.getDesRebate().add(disRebateFlow.getFlowRebateSub()));
			i += disDesignerDao.updateByModel(parDisDesigner);
		}
		
		return i;
	}
	
}